package com.stylefeng.guns.core.util;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wuchong
 */
public class DigitalUtil {


    public static void main(String[] args) {
        String text = "受球半/两球";
//        text = "一/球半";
        text = "球半";
        text = "半/一";
        System.out.println(getDigitalFromHandicap(text));
    }

    /**
     * 根据亚盘得到数字结果
     *
     * @param text
     * @return
     */
    public static double getDigitalFromHandicap(String text) {

        double digital = 0d;
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.startsWith(text, "受")) {
            stringBuilder.append("-");
        }
        String str = StringUtils.removeFirst(text, "受");
        str = StringUtils.replace(str, "球半", "1.5");
        str = StringUtils.replace(str, "平手", "0");
        str = StringUtils.replace(str, "平", "0");
        str = StringUtils.replace(str, "半", ".5");

        str = bulidTextZHToALB(StringUtils.removeEndIgnoreCase(str, "球"));
        if (str.contains("/")) {
            String[] strings = StringUtils.split(str, "/");
            str = String.valueOf((Double.parseDouble(strings[0]) + Double.parseDouble(strings[1])) / 2);
        }
        String result = stringBuilder.append(str).toString();
        try {
            digital = Double.parseDouble(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return digital;
    }

    public static String bulidTextZHToALB(String text) {
        Pattern p = Pattern.compile(numRegex);
        Matcher m = p.matcher(text);

        while (m.find()) {
            String numZH = m.group();
            if (numZH.length() != 1 || numMap.containsKey(numZH) || zhTen.equals(numZH)) {
                String numALB = NumZHToALB(numZH);
                text = text.replaceFirst(numZH, numALB);
            }
        }

        return text;
    }

    private static String NumZHToALB(String numZH) {
        int numALB = 0;
        int formIndex = 0;
        for (String unitNum : unitNumMap.keySet()) {
            int index = numZH.indexOf(unitNum);
            if (index != -1) {
                numALB += NumZHToALB(numZH.substring(formIndex, index), unitNumMap.get(unitNum));
                formIndex = index + 1;
            }
        }

        numALB += NumZHToALB(numZH.substring(formIndex), 1);
        return String.valueOf(numALB);
    }

    private static int NumZHToALB(String numZH, int unitNum) {
        int length = numZH.length();
        int numALB = 0;
        if (length != 0) {
            int fromIndex = 0;
            for (String unit : unitMap.keySet()) {
                int index = numZH.indexOf(unit, fromIndex);
                if (index != -1) {
                    fromIndex = index + unit.length();
                    String prevChar = zhOne;
                    if (index != 0 && numMap.containsKey(prevChar)) {
                        prevChar = String.valueOf(numZH.charAt(index - 1));
                    }
                    numALB += numMap.get(prevChar) * unitMap.get(unit);
                }
            }

            String lastChar = String.valueOf(numZH.charAt(length - 1));
            if (numMap.containsKey(lastChar)) {
                String pChar = zhTen;
                if (length != 1) {
                    pChar = String.valueOf(numZH.charAt(length - 2));
                    if (zhZero.equals(pChar)) {
                        pChar = zhTen;
                    }
                }
                numALB += numMap.get(lastChar) * unitMap.get(pChar) / 10;
            }
        }

        return numALB * unitNum;
    }

    private static String encodeUnicode(String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int i : utfBytes) {
            String hexB = Integer.toHexString(i);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }

    private static final String zhZero = "零";
    private static final String zhOne = "一";
    private static final String zhTen = "十";

    private static final Map<String, Integer> numMap = new HashMap<>();

    static {
        numMap.put("零", 0);
        numMap.put("一", 1);
        numMap.put("二", 2);
        numMap.put("两", 2);
        numMap.put("三", 3);
        numMap.put("四", 4);
        numMap.put("五", 5);
        numMap.put("六", 6);
        numMap.put("七", 7);
        numMap.put("八", 8);
        numMap.put("九", 9);
    }

    private static final Map<String, Integer> unitNumMap = new LinkedHashMap<>();

    static {
        unitNumMap.put("亿", 100000000);
        unitNumMap.put("万", 10000);
    }

    private static final Map<String, Integer> unitMap = new LinkedHashMap<>();

    static {
        unitMap.put("千", 1000);
        unitMap.put("百", 100);
        unitMap.put("十", 10);
    }

    private static String numRegex;

    static {
        numRegex = "[";
        for (String s : numMap.keySet()) {
            numRegex += encodeUnicode(s);
        }
        for (String s : unitMap.keySet()) {
            numRegex += encodeUnicode(s);
        }
        for (String s : unitNumMap.keySet()) {
            numRegex += encodeUnicode(s);
        }
        numRegex += "]+";
    }
}
